<template>
  <div class="content-app">
    <div class="up">
      <SecurityCheckHeader
        title="安检通道综合效率"
        :withExportButton="false"
        :withChangeButton="false"
        @search="search"
      />
    </div>
    <div class="comprehensive_cont">
      <div class="comprehensive_top">
        <div class="header">
          <p>月过检日平均件数统计</p>
          <span>单位：件/日</span>
        </div>
        <div id="comprehensive-month"></div>
      </div>
      <div class="comprehensive_bootom">
        <div class="header">
          <p>日过检小时平均件数统计</p>
          <span>单位：件/小时</span>
        </div>
        <div id="comprehensive-day"></div>
      </div>
    </div>
  </div>
</template>
<script>
import { discoloration, discolorationcops } from '@/utils/app-ecahrts'
import SecurityCheckHeader from '../components/securityCheckHeader.vue'
import { emergency } from '@/api/statistics/statistics.js'
export default {
  data() {
    return {
      queryData: {},
    }
  },
  components: {
    SecurityCheckHeader,
  },
  // mounted() {
  // },
  methods: {
    search(data) {
      this.queryData = {
        startTime: data[0][0],
        endTime: data[0][1],
        riskRating: data[1],
        agentCode: data[2],
      }
      this.relationDate(data)
    },
    //点击跳转
    echar() {
      // this.$router.push({
      //   path: '/securityCheck/waybillTable',
      //   query: this.queryData,
      // })
      let param = JSON.parse(JSON.stringify(this.queryData))
      this.$router.push({
        path: './waybillTable',
        name: 'WaybillTable',
        params: param,
      })
    },
    async relationDate(data) {
      try {
        let params = {
          beginDate: data[0][0],
          endDate: data[0][1],
          proxyLevel: data[1],
          proxyId: data[2],
          querytype: '月',
        }
        let paramspoy = {
          beginDate: data[0][0],
          endDate: data[0][1],
          proxyLevel: data[1],
          proxyId: data[2],
          querytype: '日',
        }
        const result = await emergency(params)
        const resultcopy = await emergency(paramspoy)
        result.data.sort(this.up)
        resultcopy.data.sort(this.up)
        if (result.status == 200) {
          let arr = [] //高风险
          let Low = [] //低风险
          let firs = [] //优先级
          let pt = []
          let arrx = [] //x轴
          result.data.forEach((element) => {
            arrx.push(element.date)
            element.datas.forEach((res) => {
              if (res.level === 'A') {
                if (res.dayqty && res.dayqty == 0) {
                  arr.push(Math.ceil(res.count / res.dayqty))
                } else {
                  arr.push(Math.ceil(res.count / 1))
                }
              } else if (res.level == 'B') {
                if (res.dayqty && res.dayqty != 0) {
                  Low.push(Math.ceil(res.count / res.dayqty))
                } else {
                  Low.push(Math.ceil(res.count / 1))
                }
              } else if (res.level == 'C') {
                if (res.dayqty && res.dayqty != 0) {
                  firs.push(Math.ceil(res.count / res.dayqty))
                } else {
                  firs.push(Math.ceil(res.count / 1))
                }
              } else if (res.level == 'D') {
                if (res.dayqty && res.dayqty != 0) {
                  pt.push(Math.ceil(res.count / res.dayqty))
                } else {
                  pt.push(Math.ceil(res.count / 1))
                }
              }
            })
            // console.log(arr);
          })
          let datas = []
          for (let i = 0; i <= arr.length; i++) {
            datas[i] = arr[i] + Low[i] + firs[i] + pt[i]
          }
          this.init(arr, arrx, Low, firs, pt, datas)
        }
        if (resultcopy.status == 200) {
          let arr = [] //高风险
          let Low = [] //低风险
          let firs = [] //优先级
          let pt = []
          let arrx = [] //x轴
          resultcopy.data.forEach((element) => {
            arrx.push(element.date)
            element.datas.forEach((res) => {
              if (res.level === 'A') {
                arr.push(Math.ceil(res.count / 24))
              } else if (res.level == 'B') {
                Low.push(Math.ceil(res.count / 24))
              } else if (res.level == 'C') {
                firs.push(Math.ceil(res.count / 24))
              } else if (res.level == 'D') {
                pt.push(Math.ceil(res.count / 24))
              }
            })
            // console.log(arr);
          })
          let datas = []
          for (let i = 0; i <= arr.length; i++) {
            datas[i] = Number(arr[i]) + Number(Low[i]) + Number(firs[i]) + Number(pt[i])
          }
          // console.log(datas)

          this.inits(arr, arrx, Low, firs, pt, datas)
        }
        // console.log(result)
      } catch (error) {
        console.log(error)
      }
    },
    up(x, y) {
      return Date.parse(x.date)- Date.parse(y.date)
    },
    //月
    init(arr, arrx, Low, firs, pt, datas) {
      let myChart = this.$echarts.init(document.getElementById('comprehensive-month')) //统计折线
      let myChartcopy = this.$echarts.init(document.getElementById('comprehensive-day')) //统计折线
      discoloration.series[0].data = arr
      discoloration.series[1].data = Low
      discoloration.series[2].data = firs
      discoloration.series[3].data = pt
      discoloration.series[4].data = datas
      discoloration.xAxis.data = arrx
      myChart.clear()
      myChart.setOption(discoloration)
      window.onresize = () => {
        this.queryData.agentLevel = a.seriesName
        myChart.resize()
      }
      myChart.on('click', (a, b) => {
        this.queryData.startTime = a.name + '-01'
        this.queryData.endTime = a.name + '-31'
        this.queryData.agentLevel = a.seriesName
        if (a.seriesName !== '总数') {
          this.echar()
        }
      })
    },
    //日
    inits(arr, arrx, Low, firs, pt, datas) {
      let myChart = this.$echarts.init(document.getElementById('comprehensive-day')) //统计折线
      discolorationcops.series[0].data = arr
      discolorationcops.series[1].data = Low
      discolorationcops.series[2].data = firs
      discolorationcops.series[3].data = pt
      discolorationcops.series[4].data = datas
      discolorationcops.xAxis.data = arrx
      myChart.clear()
      myChart.setOption(discolorationcops)
      window.onresize = () => {
        myChart.resize()
      }
      myChart.on('click', (a, b) => {
        this.queryData.startTime = a.name
        this.queryData.endTime = a.name
        this.queryData.agentLevel = a.seriesName
        if (a.seriesName !== '总数') {
          this.echar()
        }
        // this.queryData.openResult = a.value;
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.up {
  padding-top: 12px;
}
.comprehensive_cont {
  width: 100%;
  > .comprehensive_top {
    width: 100%;
    height: 37vh;
    background: #ffffff;
    box-shadow: 0px 3px 10px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    padding: 24px;
  }
  > .comprehensive_bootom {
    margin-top: 14px;
    width: 100%;
    height: 37vh;
    background: #ffffff;
    box-shadow: 0px 3px 10px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    padding: 24px;
  }
}
#comprehensive-month {
  width: 100%;
  height: 90%;
}
#comprehensive-day {
  width: 100%;
  height: 90%;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  > p {
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #101116;
    margin: 0 0 24px 0;
  }
  > span {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #101116;
  }
}
</style>